package com.wx.wxapp.service;

import com.wx.wxapp.dao.AddressMapper;
import com.wx.wxapp.dao.PetMapper;
import com.wx.wxapp.dao.PetimageMapper;
import com.wx.wxapp.dao.UserMapper;
import com.wx.wxapp.entity.Address;
import com.wx.wxapp.entity.Pet;
import com.wx.wxapp.entity.Petimage;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AddressService {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /*
     *   功能：存储用户位置信息
     *   参数：
     *       latitude:经度
     *       longitude:维度
     *       openid:用户openid
     *   输出:
     * */
    public void SaveCoordinate(BigDecimal latitude, BigDecimal longitude, String openid){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
        Address address = new Address();
        address = addressMapper.selectByOpenId(openid);
        if (null != address){
            System.out.println("1");
            address.setLatitude(latitude);address.setLongitude(longitude);address.setOpenid(openid);
            addressMapper.updateByPrimaryKey(address);
        }else {
            Address address1 = new Address();
            System.out.println("2");
            address1.setLatitude(latitude);address1.setLongitude(longitude);address1.setOpenid(openid);
            addressMapper.insert(address1);
        }

        sqlSession.close();
    }


    /*
     *   功能：获取3公里内的所有数据
     *   参数：
     *       latitude:经度
     *       longitude:维度
     * */
    public List<Map> GetCurrentPet(BigDecimal latitude, BigDecimal longitude){
        List<Map> lists = new ArrayList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<Address> addresses = addressMapper.selectLocalPet(latitude,longitude);
        for(Address address : addresses){
            Map map = new HashMap();
            map.put("latitude",address.getLatitude());map.put("longitude",address.getLongitude());
            int id = userMapper.selectByOpenid(address.getOpenid()).getId();
            map.put("id",id);
            lists.add(map);
        }
        sqlSession.close();
        return lists;
    }
//    public List<Map> GetCurrentPet(BigDecimal latitude, BigDecimal longitude){
//        List<Map> lists = new ArrayList<>();
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
//        PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
//        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//        PetimageMapper petimageMapper = sqlSession.getMapper(PetimageMapper.class);
//        List<Address> addresses = addressMapper.selectLocalPet(latitude,longitude);
//        for(Address address : addresses){
//            System.out.println("2"+address);
//            Map map = new HashMap();
//            map.put("latitude",address.getLatitude());map.put("longitude",address.getLongitude());
//            int id = userMapper.selectByOpenid(address.getOpenid()).getId();
//            List<Pet> pet = petMapper.selectByUserId(id);
//            map.put("pet",pet);
//            List<Petimage> petimages = petimageMapper.selectByUserId(id);
//            map.put("petimage",petimages);
//            lists.add(map);
//        }
//        sqlSession.close();
//        return lists;
//    }
}
